package org.bigwinner.flinkLearning.utils;

import org.bigwinner.flinkLearning.Constants.ConstantsValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;

/**
 * @author: IT大狮兄
 * @date: 2021/7/30 下午3:44
 * @version: 1.0.0
 * @description: jdbc工具类
 */
public class JdbcUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(JdbcUtils.class);
    private static String dirver = ConstantsValue.MYSQL_DRIVER;
    private static String url = ConstantsValue.MYSQL_URL;
    private static String user = ConstantsValue.MYSQL_USER;
    private static String password = ConstantsValue.MYSQL_PASS;

    static {
        try {
            Class.forName(dirver);
        } catch (Exception e) {
            LOGGER.error("error: {}", e);
        }
    }

    /**
     * 返回连接对象
     *
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            System.out.println("url: " + url + "\nuser: "+ user + "\npassword: " + password);
            connection = DriverManager.getConnection(url, user, password);
            //设置手动提交
            connection.setAutoCommit(false);

        } catch (SQLException e) {
            LOGGER.error("get connection error: {}", e);
        }
        return connection;
    }

    /**
     * 提交事务
     *
     * @param conn MySQL连接
     */
    public static void commit(Connection conn) {
        if (conn != null) {
            try {
                conn.commit();
            } catch (SQLException e) {
                LOGGER.error("commit error: {}", e);
            } finally {
                close(conn);
            }
        }
    }


    /**
     * 事务回滚
     *
     * @param conn
     */
    public static void rollback(Connection conn) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e) {
                LOGGER.error("rollback error: {}", e);
            } finally {
                close(conn);
            }
        }
    }

    /**
     * 释放资源
     *
     * @param conn
     */
    private static void close(Connection conn) {
        if (null != conn) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                LOGGER.error("connection close error: {}", throwables);
            }
        }
    }


    /**
     * 释放资源
     *
     * @param conn
     * @param stat
     */
    public void close(Connection conn, Statement stat) {
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 释放资源
     *
     * @param conn
     * @param stat
     * @param rs
     */
    public void close(Connection conn, Statement stat, ResultSet rs) {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
